new Vue({
  el: '#app',
  data: {
    ossImgUrl: ossImgUrl,//oss图片域名
    env: '',//判断环境
    screenHeight: document.documentElement.clientHeight,     // 当前高度
    originHeight: document.documentElement.clientHeight,     //默认高度，用作比较
    isOriginHeight: true,    //当前高度是否仍等于默认高度
    codebtnMsg: "发送验证码",
    timer: "",//定时器
    clickFlag: true,//避免重复点击
    model: "",// 2 安卓 3 ios 
    version: "",// 区分是否在mmkapp里，判断是否显示支付宝支付方式
    company_name: "",//分公司名称
    company_id: "",//分公司ID
    sign_price: "",//签约金额
    pay_status: "",//支付状态 0 未支付 1已支付
    pay_type: "",//支付方式 0未支付 1微信 2pos机 3支付宝 4线下
    phone: "",//手机号
    code: "",//验证码
    super_code: "",//推荐码
    referee_phone: '',//上级手机号
    referee_name: '',//上级名称
    isReadonly: false,//推荐人姓名是否可编辑
    isAgree: false,//是否同意协议
    is_edit: true,//是否可编辑
    flag: false,//下一步是否可点击
  },
  watch: {
    screenHeight(val) {
      if (this.originHeight - val > 60) {    //这里以防万一改了下判断形式，当屏幕高度减少了60px以上时才隐藏footer
        this.isOriginHeight = false;
      } else {
        this.isOriginHeight = true;
      }
    }
  },
  created() {
    clearInterval(this.timer);
    this.codebtnMsg = "发送验证码",
      this.timer = "";
    this.clickFlag = true;
    // 获取地址栏参数
    if (getUrlParam("company_name")) {
      var t = getUrlParam("company_name");
      this.company_name = decodeURIComponent(decodeURI(t));
    }
    if (getUrlParam("company_id")) {
      this.company_id = getUrlParam("company_id");
    }
    if (getUrlParam("sign_price")) {
      this.sign_price = getUrlParam("sign_price");
    }
    if (getUrlParam("model")) {
      this.model = getUrlParam("model");
    }
    if (getUrlParam("version")) {
      this.version = getUrlParam("version");
    }
    // 判断是否有手机号
    if (local.get("phone")) {
      this.queryInfo(local.get("phone"));
    }
  },
  mounted() {
    // 判断是否在小程序内
    var u = window.navigator.userAgent;
    var that = this;
    if (/MicroMessenger/i.test(u)) {//判断是否是微信环境
      wx.miniProgram.getEnv(res => {
        if (res.miniprogram) {// 小程序
          that.env = 'mp';
        }
      })
    }
    // 解决底部被键盘顶起的问题
    window.onresize = () => {
      return (() => {
        this.screenHeight = document.documentElement.clientHeight;
      })()
    }
  },
  destroyed() {
    clearInterval(this.timer);
    this.codebtnMsg = "发送验证码",
      this.timer = "";
    this.clickFlag = true;
  },
  methods: {
    // 根据手机号 查询基本信息
    queryInfo: function (tel) {
      var that = this;
      var phone = tel.replace(/\s*/g, "");
      if (!phone) { return }
      get("api/signUsers/" + phone).then(function (res) {
        if (res.data) {
          that.phone = phone?phone:'';//自动填入手机号
          that.is_edit = res.data.is_edit;
          that.pay_status = res.data.pay_status;
          that.pay_type = res.data.pay_type;
          that.referee_phone = res.data.referee_phone;
          that.referee_name = res.data.referee_name;
          that.isReadonly = res.data.referee_name ? true : false;
          if (res.data.company_name) {
            that.company_name = res.data.company_name;
          }
          if (res.data.company_id) {
            that.company_id = res.data.company_id;
          }
          if (res.data.sign_price) {
            that.sign_price = res.data.sign_price;
          }
          that.mustClick();
        }
      })
    },
    // 获取手机号验证码
    getCode: function () {
      if (!this.clickFlag) { return }
      var that = this;
      var phone = this.phone.replace(/\s*/g, "");
      if (!phone) {
        mui.toast('请输入手机号');
        return
      }
      get("api/sms/" + phone).then(function (res) {
        //提示信息
        mui.toast('发送成功');
        var time = 60;
        that.timer = setInterval(function () {
          that.clickFlag = false;
          time--;
          that.codebtnMsg = time + "秒后可重新发送";
          if (time <= 0) {
            that.codebtnMsg = "重新发送"
            clearInterval(that.timer);
            that.clickFlag = true;
          }
        }, 1000);
      })
    },
    // 根据邀请码/手机号 获取推荐人姓名
    getRefereeName: function (codetel) {
      var that = this;
      var referee_phone = codetel.replace(/\s*/g, "");
      if (!referee_phone) { return }
      get("api/user/" + referee_phone).then(function (res) {
        if (res.data && res.data.realname) {
          that.referee_name = res.data.realname;
          that.isReadonly = true;
          that.mustClick();
        } else {
          that.referee_name = ""
          that.isReadonly = false;
        }
      })
    },
    // 失去焦点验证下一步是否可点击
    mustClick: function () {
      if (!this.company_id) {
        this.flag = false
      } else if (!this.phone.replace(/\s*/g, "")) {
        this.flag = false
      } else if (!this.code.replace(/\s*/g, "")) {
        this.flag = false
      } else if (!this.referee_phone.replace(/\s*/g, "")) {
        this.flag = false
      } else if (!this.referee_name) {
        this.flag = false
      } else {
        this.flag = true
      }
    },

    // 下一步
    submit: function () {
      // 支付状态为1已支付||支付方式为4线下支付->提示去小程序
      if (this.pay_status == 1 || this.pay_type == 4) {
        var btnArray = ['确定'];
        mui.confirm("", "<span style='font-size:13px;color:#333333;'>您已在线支付了签约金额或是选择了线下支付，请在微信中搜索“美美咖美学设计师”小程序，用签约手机号登录小程序后继续完成签约</span>", btnArray, function (e) { })
        return;
      }
      if (!this.flag) { return }
      if (!this.isAgree) {
        var btnArray = ['确定'];
        mui.confirm("", "<span style='font-size:14px;color:#333333;'>请先阅读并同意《特约经销合作协议》《特约经销商入驻合作协议-门店版》</span>", btnArray, function (e) { })
        return;
      }
      var that = this;
      post("api/signUsers", {
        company_id: this.company_id,
        phone: this.phone.replace(/\s*/g, ""),
        code: this.code.replace(/\s*/g, ""),
        super_code: this.referee_phone.replace(/\s*/g, ""),
        referee_name: this.referee_name,
        sign_price: this.sign_price
      }).then(function (res) {
        if (res.data) {
          // 缓存手机号
          local.set("phone", res.data.phone);
          if (res.data.pay_status == 0) {//未支付->跳确认付款页
            window.location.href = "confirm_pay.html?&sign_price=" + res.data.sign_price + "&id=" + res.data.id + "&model=" + that.model + "&version=" + that.version;
          } else if (res.data.pay_status == 1) {//已支付->跳支付成功页
            window.location.href = "pay_success.html?phone=" + res.data.phone;
          }
        }
      })
    },
    // 点击查看协议
    toXieyi: function (e) {
      switch (e) {
        case 1://《特约经销商合作协议》
          window.location.href = "special_xieyi.html?sign_price=" + this.sign_price;
          break;
        case 2://《特约经销商入驻合作协议-门店版》
          window.location.href = "special_rzxieyi.html?sign_price=" + this.sign_price;
          break;
      }
    }
  }
})